package com.ncomz.cspace.login.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ncomz.cspace.login.service.LoginService;
import com.ncomz.cspace.members.dao.MembersDAO;


/**
 * 
 * 1. ClassName		: LoginController
 * 2. FileName		: LoginController.java
 * 3. Package		: com.ncomz.cspace.login.controller
 * 4. Comment		: id, password 체크 후 맞으면 세션등록
 * 5. 작성자		: 이석균
 * 6. 작성일		: 2013. 8. 21. 오후 12:13:02
 */
@Controller
public class LoginController {
	
	@Autowired
	LoginService loginService;
	MembersDAO mbDAO;
	
	private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	
	/** 
	 * 
	 * 1. MethodName	: login
	 * 2. ClassName		: LoginController
	 * 3. 작성자		: 이석균
	 * 4. 작성일		: 2013. 8. 21. 오후 12:14:19
	 * 5. Comment		: id, password 체크 후 맞으면 세션 등록
	 * @param param
	 * @return int
	 * @param id
	 * @param pw
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login.do")
	@ResponseBody
	public int login(String id, String pw, HttpSession session){
				
		HashMap<String, String> hstParam = new HashMap<String, String>();
		hstParam.put("member_id", id);
		hstParam.put("member_password", pw);
		
		Integer loginIdentify = Integer.parseInt(loginService.loginCheck(hstParam));
		
		
		if (loginIdentify == 1){
			session.setAttribute("id", id);
			session.setAttribute("pw", pw);
		}
				
		
		return loginIdentify;
	}
	
	
	/**
	 * 
	 * 1. MethodName	: loginpage
	 * 2. ClassName		: LoginController
	 * 3. 작성자		: 이석균
	 * 4. 작성일		: 2013. 8. 22. 오후 12:15:25
	 * 5. Comment		: 로그인 되어있으면 메인페이지로, 로그인되어있지 않으면 로그인페이지로 이동
	 * @param param
	 * @return ModelAndView
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/loginpage.do")
	public ModelAndView loginpage(HttpSession session){
		
		ModelAndView mav = new ModelAndView();
		
		String member_id = (String)session.getAttribute("id");
		
		if ( member_id != null) {		//session check
			
			mav.setViewName("redirect:/main.do");
			return mav;
		}
		
		mav.setViewName("/login/loginpage");

		return mav;
	}
}